﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Basic;
using System.Data.OracleClient;
using System.Data;
using System.Collections;

namespace CardLogistics.DAL.Basic
{
    /// <summary>
    /// 版面数据访问类
    /// </summary>
    public class Basic_FormatData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_basic_format", StrFields, "formatid", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" formatid = '" + id + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Basic_Format GetModel(string rId)
        {
            Basic_Format model = new Basic_Format();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["FORMATID"] != null)
                {
                    model.FORMATID = row["FORMATID"].ToString();
                }
                if (row["FORMATNAME"] != null)
                {
                    model.FORMATNAME = row["FORMATNAME"].ToString();
                }
                if (row["FORMATMODE"] != null)
                {
                    model.FORMATMODE = row["FORMATMODE"].ToString();
                }
                if (row["ATTURL"] != null)
                {
                    model.ATTURL = row["ATTURL"].ToString();
                }
                if (row["ADDTIME"] != null && row["ADDTIME"].ToString() != "")
                {
                    model.ADDTIME = DateTime.Parse(row["ADDTIME"].ToString());
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null)
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["STATUS"] != null && row["STATUS"].ToString() != "")
                {
                    model.STATUS = int.Parse(row["STATUS"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = int.Parse(row["ISDELETE"].ToString());
                }
                if (row["SuppId"] != null)
                {
                    model.SuppId = row["SuppId"].ToString();
                }

            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Basic_Format model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
                    new OracleParameter("v_formatname", OracleType.VarChar,200),
                    new OracleParameter("v_formatmode", OracleType.VarChar,200),
                    new OracleParameter("v_atturl", OracleType.VarChar,500),
                    new OracleParameter("v_lastupdateuser", OracleType.VarChar,50),
                    new OracleParameter("v_status", OracleType.Number),
                    new OracleParameter("v_isdelete", OracleType.Number),
                    new OracleParameter("v_SuppId", OracleType.VarChar,50)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.FORMATNAME;
                parameters[2].Value = model.FORMATMODE;
                parameters[3].Value = model.ATTURL;
                parameters[4].Value = model.LASTUPDATEUSER;
                parameters[5].Value = model.STATUS;
                parameters[6].Value = model.ISDELETE;
                parameters[7].Value = model.SuppId;
                string StrOutPar = DbHelperSQL.RunProcedure("P_basic_format_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Basic_Format model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
                    new OracleParameter("v_formatid", OracleType.VarChar,50),
                    new OracleParameter("v_formatname", OracleType.VarChar,200),
                    new OracleParameter("v_formatmode", OracleType.VarChar,200),
                    new OracleParameter("v_atturl", OracleType.VarChar,500),
                    new OracleParameter("v_lastupdateuser", OracleType.VarChar,50),
                    new OracleParameter("v_status", OracleType.Number),
                    new OracleParameter("v_SuppId", OracleType.VarChar,50)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.FORMATID;
                parameters[2].Value = model.FORMATNAME;
                parameters[3].Value = model.FORMATMODE;
                parameters[4].Value = model.ATTURL;
                parameters[5].Value = model.LASTUPDATEUSER;
                parameters[6].Value = model.STATUS;
                parameters[7].Value = model.SuppId;
                string StrOutPar = DbHelperSQL.RunProcedure("P_basic_format_UPDATE", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("v_formatid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_basic_format_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
